package com.medical.admin.patient.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.medical.admin.patient.common.dto.LogisticsDto;
import com.medical.admin.patient.entity.Logistics;
import com.medical.admin.patient.mapper.LogisticsMapper;
import com.medical.admin.patient.service.LogisticsService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LogisticsServiceImpl implements LogisticsService {
    @Resource
    private LogisticsMapper logisticsMapper;

    @Override
    public Integer addLogistics(LogisticsDto logisticsDto) {
        int count = logisticsMapper.insert(BeanConvertUtils.convertTo(logisticsDto, Logistics::new));
        if (count <= 0){
            throw new ServiceException(ResponseCode.LOGISTICS_ADD_ERROR);
        }
        return count;
    }

    @Override
    public Integer removeLogistics(Long logisticsId) {
        int count = logisticsMapper.deleteById(logisticsId);
        if (count <= 0){
            throw new ServiceException(ResponseCode.LOGISTICS_DELETE_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyLogistics(LogisticsDto logisticsDto) {
        int count = logisticsMapper.updateById(BeanConvertUtils.convertTo(logisticsDto, Logistics::new));
        if (count <= 0){
            throw new ServiceException(ResponseCode.LOGISTICS_UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public LogisticsDto searchLogistics(Long patientId) {
        Logistics logistics = logisticsMapper.selectById(patientId);
        if (ObjectUtil.isEmpty(logistics)){
            throw new ServiceException(ResponseCode.LOGISTICS_SELECT_ERROR);
        }
        return BeanConvertUtils.convertTo(logistics, LogisticsDto::new);
    }

    @Override
    public LogisticsDto detailLogistics(Long patientId) {
        LogisticsDto logisticsDto = logisticsMapper.selectByPatientId(patientId);
        if (ObjectUtil.isEmpty(logisticsDto)){
            throw new ServiceException(ResponseCode.LOGISTICS_DETAIL_SELECT_ERROR);
        }
        return logisticsDto;
    }
}
